package yeliuhuishi.GroupingComparator;

import lombok.Data;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.jetbrains.annotations.NotNull;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

@Data
public class OrderBean implements WritableComparable<OrderBean> {
    private Text item;
    private DoubleWritable amount;

    public void set(Text item, DoubleWritable amount) {
        this.item = item;
        this.amount = amount;
    }

    public int compareTo(@NotNull OrderBean o) {
        int cmp = this.item.compareTo(o.getItem());
        if (cmp == 0) {
            cmp = -this.amount.compareTo(o.getAmount());
        }
        return cmp;
    }

    public void write(DataOutput out) throws IOException {
        out.writeUTF(item.toString());
        out.writeDouble(amount.get());
    }

    public void readFields(DataInput in) throws IOException {
        String readUTF = in.readUTF();
        double readDouble = in.readDouble();

        this.item = new Text(readUTF);
        this.amount = new DoubleWritable(readDouble);
    }

    @Override
    public String toString() {
        return item.toString() + "\t" + amount.get();
    }
}
